package com.wigem.approve.controller;

import com.wigem.approve.service.DaqEnterpriseService;
import com.wigem.approve.service.DaqEstateService;
import com.wigem.framework.common.pojo.PageResult;
import com.wigem.framework.common.util.web.response.Result;
import com.wigem.framework.common.util.web.response.ResultController;

import java.util.List;

import com.wigem.approve.entity.DaqPolicyEntity;
import com.wigem.approve.service.DaqPolicyService;
import com.wigem.approve.vo.DaqPolicyVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.swing.text.html.parser.Entity;

/**
 * 楼宇政策申报Controller
 *
 * @author DH
 * @date 2024-05-27
 */
@RestController
@RequestMapping("/approve/policy")
public class DaqPolicyController extends ResultController {


    @Resource
    private DaqPolicyService daqPolicyService;
    @Resource
    private DaqEstateService daqEstateService;
    @Resource
    private DaqEnterpriseService daqEnterpriseService;

    /**
     * 查询楼宇政策申报列表
     */
    @ResponseBody
    @GetMapping("/query")
    public Result getList(DaqPolicyVo daqPolicyVo) {

        return success(daqPolicyService.getList(daqPolicyVo));
    }

    /**
     * 查询楼宇政策申报列表
     */
    @ResponseBody
    @GetMapping("/query/page")
    public Result getPageList(DaqPolicyVo daqPolicyVo) {
        PageResult<DaqPolicyEntity> pageInfo = daqPolicyService.getPageList(daqPolicyVo);
        return success(pageInfo);
    }

    /**
     * 查询楼宇政策申报实体
     */
    @ResponseBody
    @GetMapping("/query/{id}")
    public Result getEntity(@PathVariable("id") String id) {
        return success(daqPolicyService.getEntity(id));
    }


    /**
     * 楼宇政策申报新增
     */
    @ResponseBody
    @PostMapping
    public Result add(@RequestBody DaqPolicyVo daqPolicyVo) {
        daqPolicyService.add(daqPolicyVo);
        return success("保存成功");
    }

    /**
     * 楼宇政策申报修改
     */
    @ResponseBody
    @PostMapping("/update")
    public Result update(@RequestBody DaqPolicyVo daqPolicyVo) {
        daqPolicyService.update(daqPolicyVo);
        return success("修改成功");
    }


    /**
     * 新增保存
     */
    @ResponseBody
    @PostMapping("/save")
    public Result save(@RequestBody DaqPolicyVo daqPolicyVo) {
        String result = daqPolicyService.save(daqPolicyVo);
        return success("保存成功！", result);
    }

    /**
     * 提交验证
     */
    @ResponseBody
    @GetMapping("/submit/validate/{id}")
    public Result submitValidate(@PathVariable("id") String id) {
        Boolean result = daqPolicyService.validateEstateAndEnterprise(id);
        return result ? success("验证成功！", true) : success("验证失败！", false);
    }

    /**
     * 提交
     */
    @ResponseBody
    @GetMapping("/submit/{id}")
    public Result submit(@PathVariable("id") String id) {
        int result = daqPolicyService.submit(id);
        return result > 0 ? success("提交成功！", true) : success("提交失败！", false);
    }


    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") String id) {
        daqPolicyService.delete(id);
        return success("删除成功！");
    }

    /**
     * 移除/逻辑删除
     */
    @ResponseBody
    @GetMapping("/remove/{id}")
    public Result remove(@PathVariable("id") String id) {
        DaqPolicyEntity daqPolicyEntity = daqPolicyService.getEntity(id);
        daqEstateService.removeByPolicyId(daqPolicyEntity.getEstateId());
        daqEnterpriseService.removeByPolicyId(daqPolicyEntity.getEstateId());
        daqPolicyService.remove(id);
        return success("删除成功！");
    }
}
